<?php
/**
 * OS CRM SYSTEM - An open source application development framework for PHP 4.3.2 or newer
 *
 * CLASS - MEMBERS
 * 
 * @name      members
 * @package		OS CRM
 * @category  Class
 * @author		CRM SYSTEM Dev Team
 * @copyright	Copyright (c) 2010 - 2012, CRM SYSTEM
 * @since     Version 1.0
 * @filesource
 * @access    public 
 */

// ------------------------------------------------------------------------
class members extends Controller {

  /*common class varibale*/
  var $c 	= 'members';//current controler
	var $p_c = '';//parent controller
  
  //set db - listing data
	var $e_list_limit 	= '10';
	var $e_list_offset 	= '0';
	var $e_list_order   = 'asc';
	var $e_list_order_by= ''; 
  
  /*message strings*/
  var $valid_success 	= '';
  var $valid_errors 	= '';
  var $valid_notice 	= '';
  
  /**
   * members  - constructor of the class
   * 
   * @access public
   * @param  NA
   * @return void
   */
	function members()
	{
		parent::Controller();	
	}
	
  /** 
	 * Index
	 *
	 * @desc NA
	 * @access public/private
	 * @param  NA	 
	 * @return void/mixed
	 */
	function index()
	{
    $this->reg();
	}
  
  
  /** 
	 * reg - registration
	 *
	 * @desc NA
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
  function reg()
	{
    $id = 0;
		$token = random_string();
    $post_arr = array();
    $post_arr['os_mem_ip_add'] = $_SERVER['SERVER_ADDR']; 
    if ($_POST) {
      $post_arr = $this->_set_post_data();   
    }
    
		$data = array();
		$data['cont_title'] = 'Member Registration';
    
    $data['post_arr']     = $post_arr;
		
		/*set crontroller info*/
		$data['c'] 	= $this->c;
		$data['p_c'] 	= $this->p_c;
		    
    /*set form actions*/
    $data['frm_name']   = 'frmmemberadd';
    $data['frm_action'] = site_url( $this->c .'/insert');
    $data['frm_token']  = $token;
    $data['frm_mode']   = 'add';
    $data['frm_id']      = $id;
            
    $this->session->set_userdata('frm_token', $token);
    
    /*set message strings*/
    $data['msg_info']['valid_errors'] = $this->valid_errors;
    $data['msg_info']['valid_notice'] = $this->valid_notice;
    $data['msg_info']['valid_success'] = $this->valid_success;
    
    __display('registration',$data);
	} 
  
  /** 
	 * Insert
	 *
	 * @desc this will validate and insert a new record
	 * @access public
	 * @param  NA	 
	 * @return mixed
	 */
	function insert()
	{
		$this->load->library('form_validation');
		$login_status     = false;
		$data             = array();
    $frm_token        = $this->input->post('frm_token');
		$sess_frm_token   = $this->session->userdata('frm_token');
    //$this->session->unset_userdata('frm_token');
		$errors = $this->_val_form();     
		if(!empty($errors)){
			$this->valid_errors =  $errors;
      $this->reg();
    }else if($frm_token != $sess_frm_token){      
			$this->valid_errors =  '<p>&raquo; Sorry form cannot be posted twice, please re-enter the details</p>';
      $this->reg();
    }else{
      $data = array();   
      $mem_ip_set = $this->config->item('mem_ip_set');
      $chk_ip_info = $this->input->post('os_mem_ip_info');
      $data['os_mem_ip_info'] = (isset($mem_ip_set[$chk_ip_info])) ? $mem_ip_set[$chk_ip_info]: '2'/*login access: 2*/;
      $data['os_mem_ip_add'] = $this->input->post('os_mem_ip_add');
      $data['os_mem_ins_code'] = $this->input->post('os_mem_ins_code');
      $data['os_mem_ins_name'] = $this->input->post('os_mem_ins_name');
      $data['os_mem_ins_add1'] = $this->input->post('os_mem_ins_add1');
      $data['os_mem_ins_add2'] = $this->input->post('os_mem_ins_add2');
      $data['os_mem_ins_city'] = $this->input->post('os_mem_ins_city');
      $data['os_mem_ins_state'] = $this->input->post('os_mem_ins_state');
      $data['os_mem_ins_postcode'] = $this->input->post('os_mem_ins_postcode');
      $data['os_mem_ins_country'] = $this->input->post('os_mem_ins_country');
      $data['os_mem_ins_email'] = $this->input->post('os_mem_ins_email');
      $data['os_mem_usr'] = $this->input->post('os_mem_ins_email');      
      $data['os_mem_ins_phone'] = $this->input->post('os_mem_ins_phone');
      $data['os_mem_ins_fax'] = $this->input->post('os_mem_ins_fax');
      $data['os_mem_status'] = '1';
      $data['os_mem_created'] = date(DB_DATE_FORMAT);
      $data['os_mem_ref_ip'] = $this->input->ip_address();
      $data['os_mem_ref_agent'] = $this->input->user_agent();
      
      
      $data['os_mem_accp_terms']  = $this->input->post('os_mem_accp_terms');
      $data['os_mem_reg_type']  = $this->input->post('os_mem_reg_type');
      $data['os_mem_comments']    = filter_data($this->input->post('os_mem_comments'));
      
      $ref_url = $this->input->server('HTTP_REFERER');
      if(empty($ref_url )){
        $ref_url = $this->input->server('REQUEST_URI');
      }
      $data['os_mem_ref_url'] = $ref_url;
      
      $this->db->insert(TBL_MEMBERS, $data); 
      $last_id = $this->db->insert_id();
      //__debug($last_id);
      
      /*add invoice details*/
      $data = array();
      $data['os_mem_id'] = $last_id;
      $data['os_mem_inv_name'] = $this->input->post('os_mem_inv_name');
      $data['os_mem_inv_del'] = $this->input->post('os_mem_inv_del');
      $data['os_mem_inv_ins_name'] = $this->input->post('os_mem_inv_ins_name');
      $data['os_mem_inv_add1'] = $this->input->post('os_mem_inv_add1');
      $data['os_mem_inv_add2'] = $this->input->post('os_mem_inv_add2');
      $data['os_mem_inv_city'] = $this->input->post('os_mem_inv_city');
      $data['os_mem_inv_state'] = $this->input->post('os_mem_inv_state');
      $data['os_mem_inv_postcode'] = $this->input->post('os_mem_inv_postcode');
      $data['os_mem_inv_country'] = $this->input->post('os_mem_inv_country');
      $data['os_mem_inv_email'] = $this->input->post('os_mem_inv_email');      
      $data['os_mem_inv_phone'] = $this->input->post('os_mem_inv_phone');
      $data['os_mem_inv_fax'] = $this->input->post('os_mem_inv_fax');      
      $data['os_mem_inv_created'] = date(DB_DATE_FORMAT);  
      $this->db->insert(TBL_MEMBERS_INV, $data); 
      
      /*add admin/library details*/
      $data = array();
      $data['os_mem_id'] = $last_id;
      $data['os_mem_adm_name'] = $this->input->post('os_mem_adm_name');
      $data['os_mem_adm_desg'] = $this->input->post('os_mem_adm_desg');
      $data['os_mem_adm_dept'] = $this->input->post('os_mem_adm_dept');
      $data['os_mem_adm_email'] = $this->input->post('os_mem_adm_email');
      $data['os_mem_adm_phone'] = $this->input->post('os_mem_adm_phone');
      $data['os_mem_adm_mobile'] = $this->input->post('os_mem_adm_mobile');
      $data['os_mem_adm_fax'] = $this->input->post('os_mem_adm_fax');      
      $data['os_mem_adm_created'] = date(DB_DATE_FORMAT);  
      $this->db->insert(TBL_MEMBERS_ADM, $data); 
      
      /*add technical details*/
      $data = array();
      $data['os_mem_id'] = $last_id;
      $data['os_mem_tech_name'] = $this->input->post('os_mem_tech_name');
      $data['os_mem_tech_desg'] = $this->input->post('os_mem_tech_desg');
      $data['os_mem_tech_dept'] = $this->input->post('os_mem_tech_dept');
      $data['os_mem_tech_email'] = $this->input->post('os_mem_tech_email');
      $data['os_mem_tech_phone'] = $this->input->post('os_mem_tech_phone');
      $data['os_mem_tech_mobile'] = $this->input->post('os_mem_tech_mobile');
      $data['os_mem_tech_fax'] = $this->input->post('os_mem_tech_fax');            
      $data['os_mem_tech_created'] = date(DB_DATE_FORMAT);  
      $this->db->insert(TBL_MEMBERS_TECH, $data); 
      
      
      $this->load->plugin('phpmailer');
      /*send email to admin*/
      $subject = OS_CRM_TITLE . ' - Member Registration '. __set_date(date(DB_DATE_FORMAT), 4);
      $post_arr = $this->_set_post_data();   
      $message = $this->load->view('email_admin',array('post_arr'=>$post_arr),TRUE);
      send_smtp_email_admin($subject, $message);
      
      /*send email to user*/
      $subject = OS_CRM_TITLE . ' - Member Registration '. __set_date(date(DB_DATE_FORMAT), 4);
      $post_arr = $this->_set_post_data();   
      $message = $this->load->view('email_user',$post_arr,TRUE);
      send_smtp_email($post_arr['os_mem_ins_email'], $post_arr['os_mem_ins_name'], $subject, $message);
      
      
      redirect($this->c.'/thankyou');
    }
	}
  
  /** 
	 * Validation Form
	 *
	 * @descp NA
	 * @access private
	 * @param  NA	 
	 * @return array
	 */
	function _val_form()
	{
		$config =  array();
    
    $config[] =  array('field' => 'os_mem_ip_info',  'label' => 'IP Info',  'rules' => 'trim|required|xss_clean');
    $chk_ip = $this->input->post('os_mem_ip_info');
    if(empty ($chk_ip) || $chk_ip == 'ip'){
      $config[] =  array('field' => 'os_mem_ip_add',  'label' => 'IP Address',  'rules' => 'trim|required|valid_ip|xss_clean');
    }
    $config[] =  array('field' => 'os_mem_reg_type',  'label' => 'Registration User',  'rules' => 'trim|required|xss_clean');
    
    /*Institutional Details*/
    $config[] =  array('field' => 'os_mem_ins_code',  'label' => 'Institution Code',  'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_name',  'label' => 'Institution Name',  'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_add1',  'label' => 'Institution Add1',  'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_add2',  'label' => 'Institution Add1',  'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_city',  'label' => 'Institution City',  'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_state', 'label' => 'Institution State', 'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_postcode',  'label' => 'Institution Postcode',  'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_country',   'label' => 'Institution Country',   'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_phone',     'label' => 'Institution Phone',     'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_ins_fax',       'label' => 'Institution Fax',       'rules' => 'trim|min_length[2]|xss_clean');
    
    /*Invoice Details*/
    $config[] =  array('field' => 'os_mem_inv_name',    'label' => 'Invoice Name',      'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_del',     'label' => 'Delegation',        'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_ins_name','label' => 'Institution Name',  'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_add1',    'label' => 'Invoice Add1',      'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_add2',    'label' => 'Invoice Add1',      'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_city',    'label' => 'Invoice City',      'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_state',   'label' => 'Invoice State',     'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_postcode','label' => 'Invoice Postcode',  'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_country', 'label' => 'Invoice Country',   'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_phone',   'label' => 'Invoice Phone',     'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_inv_fax',     'label' => 'Invoice Fax',       'rules' => 'trim|min_length[2]|xss_clean');
    
    /*Adin/Library Details*/
    $config[] =  array('field' => 'os_mem_adm_name',    'label' => 'Admin/Library Name',        'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_adm_desg',    'label' => 'Admin/Library Designation', 'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_adm_dept',    'label' => 'Admin/Library Department',  'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_adm_email',   'label' => 'Admin/Library Email',       'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_adm_phone',   'label' => 'Admin/Library Phone',       'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_adm_mobile',  'label' => 'Admin/Library Mobile',      'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_adm_fax',     'label' => 'Admin/Library Fax',         'rules' => 'trim|min_length[2]|xss_clean');
    
    /*Technical Details*/
    $config[] =  array('field' => 'os_mem_tech_name',    'label' => 'Technical Name',         'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_tech_desg',    'label' => 'Technical Designation',  'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_tech_dept',    'label' => 'Technical Department',   'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_tech_email',   'label' => 'Technical Email',        'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_tech_phone',   'label' => 'Technical Phone',        'rules' => 'trim|required|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_tech_mobile',  'label' => 'Technical Mobile',       'rules' => 'trim|min_length[2]|xss_clean');
    $config[] =  array('field' => 'os_mem_tech_fax',     'label' => 'Technical Fax',          'rules' => 'trim|min_length[2]|xss_clean');
    
    $config[] =  array('field' => 'os_mem_accp_terms',     'label' => 'Terms and Conditions', 'rules' => 'trim|required|xss_clean');
    
    
		$this->form_validation->set_rules($config); 
		$this->form_validation->set_error_delimiters('<p>&raquo; ', '</p>');
		if ($this->form_validation->run() == false)
		{
			return validation_errors(); 
		}
	}	
  
  /** 
	 * thankyou - thank you message
	 *
	 * @desc NA
	 * @access public
	 * @param  NA	 
	 * @return void
	 */
  function thankyou()
	{
    $data = array();
		$data['cont_title'] = 'Member Registration';
    
		/*set crontroller info*/
		$data['c'] 	= $this->c;
		$data['p_c'] 	= $this->p_c;
		    
    __display('thankyou',$data);
	}
  
  /** 
	 * Validation Form
	 *
	 * @descp NA
	 * @access private
	 * @param  NA	 
	 * @return array
	 */
	function _set_post_data()
	{
    $var = array();    
    if(isset($_POST)){      
      foreach($_POST as $key => $val){
        $var[$key] = $this->input->post($key); 
      }
    }    
    return $var;
  }
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */